package com.study.www.config;

import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * 多数据源：数据源1
 * @author : 89007337.zhuyanpeng
 **/
@Configuration
public class Test1DataSourceConfig {

    public static final String TEST1SQLSESSIONFACTORY="test1SqlSessionFactory";



    @Bean
    @Primary
    public DataSource test1DataSource(DBConfig1 config1)  {
        MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(config1.getUrl());
        //mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXADataSource.setUser(config1.getUsername());
        mysqlXADataSource.setPassword(config1.getPassword());
        //Atomikos 事务
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXADataSource);
        atomikosDataSourceBean.setXaDataSourceClassName(mysqlXADataSource.getClass().getName());
        atomikosDataSourceBean.setMaxPoolSize(config1.getMaxPoolSize());
        atomikosDataSourceBean.setMinPoolSize(config1.getMinPoolSize());
        atomikosDataSourceBean.setUniqueResourceName("test1DataSource");
        System.out.println(mysqlXADataSource.getClass().getName());
        atomikosDataSourceBean.setXaDataSourceClassName(mysqlXADataSource.getClass().getName());
        return atomikosDataSourceBean;
    }


    /**
    *  SqlSession 工厂
    */
    @Bean(TEST1SQLSESSIONFACTORY)
    @Primary
    public SqlSessionFactory test1SqlSessionFactory(@Value("${mybatis.test1.mapperLocations}")  String test1MapperLocations,
                                                    @Value("${mybatis.test1.type-aliases-package}")  String test1TypeAliasesPackage,
                                                    @Value("${mybatis.config-location}")  String configLocation,
                                                   @Qualifier("test1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations( new PathMatchingResourcePatternResolver()
                .getResources(test1MapperLocations));
        //mybatis Config 配置
        ClassPathResource classPathResource = new ClassPathResource(configLocation);
        sqlSessionFactoryBean.setConfigLocation(classPathResource);
        sqlSessionFactoryBean.setTypeAliasesPackage(test1TypeAliasesPackage);
        return sqlSessionFactoryBean.getObject();
    }

}
